import axios from './axios'
import qs from 'qs'
import { baseUrl } from '@/config/config'
// 获取用户列表
export const userGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/users',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 新增用户
export const userPost = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/user',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 修改用户
export const userPut = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/user' + '/' + params.id,
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 用户删除
export const userDelete = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/user/' + params.id,
      method: 'Delete',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 重置密码
export const resetPassword = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/user/password/reset/' + params.id,
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 修改密码
export const updatePassword = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/user/password',
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 角色管理
export const roleGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/roles',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 角色新增
export const rolePost = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/role',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 角色修改
export const rolePut = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/role/' + params.id,
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 角色删除
export const roleDelete = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/role/' + params.id,
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 角色管理
export const roleGetAll = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/roles/all',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 权限管理
export const permissionGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/permissions',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 权限新增修改
export const permissionPost = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/permission',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 权限修改
export const permissionPut = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/permission/' + params.id,
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 删除权限
export const permissionDelete = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/permission/' + params.id,
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}

// 获取当前用户所有菜单树
export const menuGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menus',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 菜单新增
export const menuPost = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menu',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 菜单修改
export const menuPut = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menu/' + params.id,
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 删除菜单
export const menuDelete = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menu/' + params.id,
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 获取当前用户所有菜单树
export const menuTreeGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menu/tree',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 获取当前用户所有菜单树列表
export const menuTreeListGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menu/tree/list/' + params.role_id,
      method: 'GET'
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 保存角色菜单
export const roleMenuSave = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/role/menu/save',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 获取当前用户所有菜单树权限列表
export const menuTreePermissionsGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/menu/tree/permissions/' + params.role_id,
      method: 'GET'
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 保存角色权限
export const rolePermissionSave = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/role/permission/save',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 获取当前用户日志
export const logGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/syslogs',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 获取字典列表
export const dictGet = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/dicts',
      method: 'GET',
      params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 新增字典
export const dictPost = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/dict',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 修改字典
export const dictPut = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/dict/' + params.id,
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
// 删除删除
export const dictDelete = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/dict/' + params.id,
      method: 'Delete',
      headers: {
        'Content-Type': 'application/json; charset=utf-8' 
      },
      data: JSON.stringify(params)
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error.data)
    })
  })
}
